package com.sdgakj.utils;

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;

//import entity.Confirm;

/**
 * Created by lihongyi on 2017/1/11.
 */
public class PdfTemplate {
    private String templatePdfPath;
    private String ttcPath;
    private String targetPdfpath;
    private String shopname;
    private String summary;
    private String buyername;
    private Date detectDate;
    //未通过的检测项的id
    private Set<String> nopass;


    public PdfTemplate() {
        super();
    }

    public PdfTemplate(String templatePdfPath, String ttcPath,
                       String targetPdfpath) {
        this.templatePdfPath= templatePdfPath;
        this.ttcPath= ttcPath;
        this.targetPdfpath= targetPdfpath;
    }

    public String getShopname() {
        return shopname;
    }

    public void setShopname(String shopname) {
        this.shopname = shopname;
    }

    public Date getDetectDate() {
        return detectDate;
    }

    public void setDetectDate(Date detectDate) {
        this.detectDate = detectDate;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public Set<String> getNopass() {
        return nopass;
    }

    public void setNopass(Set<String> nopass) {
        this.nopass = nopass;
    }

    public void templetConfirm(File file) throws Exception {

        PdfReader reader = new PdfReader(templatePdfPath);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PdfStamper ps = new PdfStamper(reader, bos);

        /*使用中文字体 */
        String fontPath=templatePdfPath.replaceAll("file/confirm.pdf","static/fonts/STXINWEI.TTF");
        BaseFont bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
        fontList.add(bf);

        AcroFields s = ps.getAcroFields();

        s.setSubstitutionFonts(fontList);

        s.setField("rzjgbh","91370800788454023Q");
        //是否选项
        for(int i=1;i<=78;i++){
            s.setField("yes"+i,"√");
        }

        for(String ss:nopass){
             int p=Integer.parseInt(ss)-72;
             s.setField("yes"+p,"");
             s.setField("no"+p,"√");
        }

        //检测日期
        String date=new SimpleDateFormat("yyyy-MM-dd").format(detectDate);
        String[] dates=date.split("-");
        System.out.println(dates[0]+"--"+dates[1]+"--"+dates[2]);
        s.setField("yyyy", dates[0]);

        s.setField("mm", dates[1]);
        s.setField("dd", dates[2]);

        s.setField("summary",summary);
        ps.setFormFlattening(true);
        ps.close();

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bos.toByteArray());
        fos.close();
    }

    /**
     * @return the templatePdfPath
     */
    public String getTemplatePdfPath() {
        return templatePdfPath;
    }

    /**
     * @param templatePdfPath the templatePdfPathto set
     */
    public void setTemplatePdfPath(String templatePdfPath) {
        this.templatePdfPath= templatePdfPath;
    }

    /**
     * @return the ttcPath
     */
    public String getTtcPath() {
        return ttcPath;
    }

    /**
     * @param ttcPath the ttcPath to set
     */
    public void setTtcPath(String ttcPath) {
        this.ttcPath= ttcPath;
    }

    /**
     * @return the targetPdfpath
     */
    public String getTargetPdfpath() {
        return targetPdfpath;
    }

    /**
     * @param targetPdfpath the targetPdfpath toset
     */
    public void setTargetPdfpath(String targetPdfpath) {
        this.targetPdfpath= targetPdfpath;
    }

    public String getBuyername() {
        return buyername;
    }

    public void setBuyername(String buyername) {
        this.buyername = buyername;
    }
}
